// pages/flea/mySend/index.js
var app = getApp(), fetchApi = require("../../../../../utils/httpPost.js"), 
common_Js = require("../../../../../component/common.js");
const { default: Toast } = require("../../../../../component/vant/toast/toast.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab:0,
    recordsList:[],
    pageIndex:1,
    pageSize:10,
    loading: false,
    netWork: true,
    isLoading: true,
    netWork:false,
    hasData:false,
    info_type:0,
    fromIndex:0,
    isNewReq:1,
    approveType:0,
    approveList:[],
    is_admin:0,
    // states: [0,2,3,4]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.is_admin){
      this.data.is_admin = parseInt(options.is_admin);
    }
    if(options.from){
      this.data.fromIndex = parseInt(options.from);
    }
    this.setData({
      currentTab: this.data.fromIndex,
    });
  },
  handleChange({ detail }) {
    console.log('handleChange',this.data.isNewReq)
    this.data.currentTab = detail.index;
    this.initRequest(detail.index);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.isNewReq = 1;
    if(this.data.fromIndex >= 0){
      this.initRequest(this.data.fromIndex)
    }else{
      this.initRequest(0)
    }
  },
  initRequest(currentTab){
    var that = this;
    that.data.pageIndex = 1;
    that.setData({
      currentTab: currentTab,
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      recordsList:[]
    });
    that.onHttpInit()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    var url = '/examine/approve/record/list';
    fetchApi.httpPost(url, {
      // type: that.data.approveType,
      state: that.data.currentTab,
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex
    }, function (res) {
      that.data.isNewReq = 0;
      if (res.data.code == 0) {
        that.setData({
          approveList: res.data.data.approveList
        })

        if (res.data.totalcount > 0) {
          that.data.pageIndex += 1;
          console.log("打卡列表", res)
          var records = res.data.data.records;
          if(records.length > 0){
            for (var index in records) {
              that.data.recordsList.splice(that.data.recordsList.length, 0, records[index]);
            }
            that.setData({
              hasData: true,
              recordsList: that.data.recordsList
            })
          }
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  goRecordDetail(e){
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../detail/index?id=' + item.id,
    })
  },
  goCopy(e){
    console.log('goCopy', e)
    let item = e.currentTarget.dataset.item;
    let that = this;
    // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
    wx.setClipboardData({//复制文本
      data: item.approveNo,
      success: function (res) {
          Toast('复制成功')
      },fail(res){
        console.log(res)
        Toast('复制失败')
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作  下拉刷新
   */
  onPullDownRefresh: function () {
    this.initRequest(this.data.currentTab)
  },
  preImg: function (t) {
    console.log("preSunImg", t)
    var a = t.currentTarget.dataset.imageindex, e = t.currentTarget.dataset.imgdata || [];
    var c = [];
    for (var index in e) {
      c.push(e[index])
    }
    common_Js.preImg(a, c)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.onHttpInit()
  },
})